import { observable, action } from 'mobx';
import { WeaTableNew } from 'comsMobx';
import { WeaTools } from 'ecCom';
import { message, Modal } from 'antd';
import ListBaseStore from './ListBaseStore';
import Apis from '../apis/car';
import { WeaLocaleProvider } from 'ecCom';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
/**
 * 车辆查询Store
 */
export default class CarManagerStore extends ListBaseStore {
  constructor() {
    super();
    this.selectedKey = 'carlist';
  }

  // 车辆类型数据
  @observable typeList = {
    carTypeListData: [],
    status: '',
  };

  @observable carinfoDatas = {};
  @observable operatelevel = 0;
  @observable timer;
  @observable shareListStore = new TableStore(); // 共享范围
  @observable status = {
    rowSelectIds: [], // check选择行id
    selectId: '', // 单独行后操作
    createOrEdit: 0, // 0:新建 1:编辑
    fields: [], // 会议室页面定义
    editSelKey: 0, // 编辑选择tab页
    shareFields: [], // 新增共享范围
    showShare: false, // 新增共享范围是否显示
    createSelectIds: [],
    createRoomLoading: false,
  };
  // 车辆共享界面定义
  getCarShareFields = (params) => {
    Apis.getCarShareFields(params)
      .then((result) => {
        this.status = { ...this.status, shareFields: result.fields };
      });
  };

  // 添加车辆共享
  saveShare = (params) => {
    Apis.saveShare(params)
      .then(() => {
        this.status = { ...this.status, showShare: false };
        this.getShareList({ id: this.status.selectId, preview: 0 });
      });
  };

  // 车辆共享列表
  getShareList = (params) => {
    Apis.getShareList(params)
      .then((result) => {
        this.shareListStore.getDatas(result.sessionkey, 1);
      });
  };

  addShare = () => {
    this.setState({ showShare: true });
    this.getCarShareFields();
  };

  delShareConfirm = () => {
    const { createSelectIds, selectId } = this.status;
    if (!createSelectIds || createSelectIds.length == 0) {
      Modal.warning({
        title: `${getLabel(15172, '系统提示')}`,
        content: `${getLabel('501210', '请选择共享范围')}!`,
      });
    } else {
      Modal.confirm({
        title: `${getLabel(131329, '信息确认')}`,
        content: `${getLabel(82017, '确认删除？')}`,
        okText: `${getLabel(382958, '确定')}`,
        cancelText: `${getLabel(31129, '取消')}`,
        onOk: () => this.delShare({ carid: selectId, ids: createSelectIds }),
      });
    }
  };

  setState = (params = {}) => {
    const status = { ...this.status };
    Object.keys(params)
      .forEach((key) => {
        status[key] = params[key];
      });
    this.status = status;
  };

  // 删除车辆共享
  delShare = (params) => {
    Apis.delShare(params)
      .then(() => {
        this.status = { ...this.status, showShare: false, createSelectIds: [] };
        this.getShareList({ id: this.status.selectId, preview: 0 });
      });
  };

  // 加载列表数据
  @action doTableLoading = (params) => {
    this.showLoading(true);

    let resultParams = this.getFormParams();
    const commonParams = {
      carstatus: this.selectedKey ? this.selectedKey : 'carlist',
    };
    resultParams = { ...resultParams, ...commonParams, ...params };

    Apis.getCarList(resultParams)
      .then(
        (result) => {
          const sessionkey = result.sessionkey;
          this.sessionkey = sessionkey;
          this.tableStore.getDatas(sessionkey, 1);
          this.operatelevel = result.operatelevel;
          this.showLoading(false);
        },
      );
  };
  @action reloadTable = () => {
    this.tableStore.getDatas(this.sessionkey, 1);
  }

  // 初始化车辆查询页面查询条件
  @action initConditionDatas = (params) => {
    Apis.getCarSearchListCondition(params)
      .then(
        (result) => {
          const { conditioninfo } = result;
          this.conditioninfo = conditioninfo;
          this.initFormFields(conditioninfo); // 一定要先初始化initFormFields表单对象
        },
      );
  };


  // 删除车辆
  delCarinfo = (params, callback) => {
    Apis.delCarinfo(params)
      .then((result) => {
        if (params && result && result.status === '1') {
          callback();
          message.success(getLabel(385854, '删除车辆成功'));
        } else {
          message.error(getLabel(385855, '删除车辆失败'));
        }
      });
  };

  // 封存解封车辆
  depositCar = (params, callback) => {
    params.operation = 'deposit';
    const msg = params.deposit == 1 ? getLabel(22152, '解封') : getLabel(22151, '封存');
    Apis.saveorupdateCar(params)
      .then((result) => {
        if (params && result && result.status === '1') {
          callback();
          message.success(msg + getLabel(385856, '车辆成功'));
        } else {
          message.error(msg + getLabel(385857, '车辆失败'));
        }
      });
  };


  getCarDetachable = (callbackfun) => {
    Apis.getCarDetachable()
      .then((result) => {
        if (result && result.status === '1') {
          callbackfun(result);
        } else {
          message.error(getLabel(385858, '获取车辆分权信息失败'));
        }
      });
  };
}
